layui.use(['form', 'layer', 'table', 'laytpl'], function () {
	var layer = top.layui.layer,
		$ = layui.jquery,
		table = layui.table;

	window.active = {
		search: () => {
			var key = $(".searchVal").val();
			table.reload("tableListTable", {
				page: {
					curr: 1 //重新从第 1 页开始
				},
				where: {
					key: key //搜索的关键字
				}
			})
		},
		reload: () => {
			table.reload("tableListTable");
		},
		editTel: (data, obj) => {
			layer.prompt({
				value: data.tel,
				title: '修改电话',
			}, async (value, index, elem) => {
				layer.close(index);

				data.tel = value;
				await ajaxAsync('users', js.regroup(data, ['openid', 'tel']), 'POST')
				obj.update(data);
			});
		},
		editRemark: (data, obj) => {
			layer.prompt({
				value: data.remark,
				title: '修改备注',
			}, async (value, index, elem) => {
				layer.close(index);

				data.remark = value;
				await ajaxAsync('users', js.regroup(data, ['openid', 'remark']), 'POST')
				obj.update(data);
			});
		},
		editTaobaoID: (data, obj) => {
			layer.prompt({
				value: data.taobao_id,
				title: '修改备注',
			}, async (value, index, elem) => {
				layer.close(index);

				data.taobao_id = value;
				await ajaxAsync('users', js.regroup(data, ['openid', 'taobao_id']), 'POST')
				obj.update(data);
			});
		},
		addPoints: (data, obj) => {
			layer.prompt({
				value: '',
				title: '增加珠珠币',
			}, async (value, index, elem) => {
				layer.close(index);

				if (!js.isInt(value)) {
					alert('请填写数字');
					return;
				}

				let addPoints = value * 1;
				await ajaxAsync('users', js.regroup(data, ['openid', 'points'], { addPoints }), 'POST')
				data.points += addPoints;
				obj.update(data);

			});
		},
		reducePoints: (data, obj) => {
			layer.prompt({
				value: '',
				title: '减少珠珠币',
			}, async (value, index, elem) => {
				layer.close(index);

				if (!js.isInt(value)) {
					alert('请填写数字');
					return;
				}

				let addPoints = value * -1;
				await ajaxAsync('users', js.regroup(data, ['openid', 'points'], { addPoints }), 'POST')
				data.points += addPoints;
				obj.update(data);
			});
		},
		addLT: (data, obj) => {
			layer.prompt({
				value: '',
				title: '增加奖票',
			}, async (value, index, elem) => {
				layer.close(index);

				if (!js.isInt(value)) {
					alert('请填写数字');
					return;
				}

				data.lottery_ticket += value * 1;
				await ajaxAsync('users', js.regroup(data, ['openid', 'lottery_ticket']), 'POST')
				obj.update(data);

			});
		},
		reduceLT: (data, obj) => {
			layer.prompt({
				value: '',
				title: '减少奖票',
			}, async (value, index, elem) => {
				layer.close(index);

				if (!js.isInt(value)) {
					alert('请填写数字');
					return;
				}

				data.lottery_ticket += value * -1;
				await ajaxAsync('users', js.regroup(data, ['openid', 'lottery_ticket']), 'POST')
				obj.update(data);
			});
		}
	}

	//表格外按钮操作
	$('.tableHead .layui-btn').on('click', function () {
		var type = $(this).data('type');
		active[type] ? active[type].call(this) : '';
	});
	//表格按钮操作
	table.on('tool(tableList)', function (obj) {
		var type = obj.event;
		active[type] ? active[type].call(this, obj.data, obj) : '';
	});

})